home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / dev / src / WBBump_src.lha / WBBump_src / Plugins / image.wbbplugin.e < prev    next >
Encoding:
Text File  |  1999-06-30  |  6.6 KB  |  345 lines

  1. /* ***************** */
  2. /* image.wbbplugin.e */
  3. /* ***************** */
  4.  
  5.  
  6.  
  7. /*
  8.     WBBump - Bumpmapping on the Workbench!
  9.  
  10.     Copyright (C) 1999  Thomas Jensen - dm98411@edb.tietgen.dk
  11.  
  12.     This program is free software; you can redistribute it and/or modify
  13.     it under the terms of the GNU General Public License as published by
  14.     the Free Software Foundation; either version 2 of the License, or
  15.     (at your option) any later version.
  16.  
  17.     This program is distributed in the hope that it will be useful,
  18.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.     GNU General Public License for more details.
  21.  
  22.     You should have received a copy of the GNU General Public License
  23.     along with this program; if not, write to the Free Software Foundation,
  24.     Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25. */
  26.  
  27.  
  28.  
  29. /*
  30.     Example plugin for WBBump
  31.     loads an image with datatypes and uses it as bumpmap
  32. */
  33.  
  34. OPT PREPROCESS
  35.  
  36.  
  37.  
  38. LIBRARY 'image.wbbplugin', 1, 0, 'image.wbbplugin 1.0 (1/5/99)' IS
  39.     pluginInit(A0), pluginCleanup(),
  40.     pluginInitInstance(A0), pluginFreeInstance(A0),
  41.     pluginGetAttr(D1,D2,A0), pluginSetAttr(D1,D2,D3),
  42.     pluginDoAction(A0,A1,A2,A3)
  43.  
  44.  
  45.  
  46.  
  47.  
  48. /* just to put flags in top of source */
  49.  
  50. #define    ARGTEMPLATE      'NAME/A'
  51. #define    PLUGINTYPE      PLUGINTYPE_BUMPER
  52. CONST    ISMODIFIER    = FALSE                -> this is not a modifying plugin
  53. CONST    ISSTATIC    = TRUE                -> this is a static plugin (see plugin.e)
  54.  
  55.  
  56. /* needed modules */
  57.  
  58.  
  59.  
  60. MODULE    'utility',
  61.         'utility/tagitem',
  62.  
  63.         'datatypes'
  64.  
  65.  
  66. MODULE    '*/plugin_const',
  67.         '*/argparser',
  68.         '*/chunkyimage',
  69.         '*/errors'
  70.  
  71.  
  72.  
  73.  
  74. OBJECT handle
  75.     width    :    LONG
  76.     height    :    LONG
  77.     args    :    PTR TO CHAR
  78.     rdargs    :    LONG
  79.     picname    :    PTR TO CHAR
  80.     image    :    PTR TO cimg
  81.     firstrun:    LONG        -> bool
  82. ENDOBJECT
  83.  
  84.  
  85.  
  86.  
  87. DEF    lasterr=NIL
  88.  
  89.  
  90.  
  91.  
  92. PROC main()
  93.     /* library init code here */
  94.     /* this is executed in Forbid() so be carefull */
  95.     /* better use pluginInit() for most things */
  96.  
  97.     /* we need utility.library for GetTagData() */
  98.     IF (utilitybase := OpenLibrary('utility.library', 37)) = NIL THEN RETURN FALSE
  99.  
  100.  
  101.  
  102. ENDPROC
  103.  
  104.  
  105.  
  106. PROC close()
  107.     /* free stuff allocated in main() here */
  108.  
  109.     IF utilitybase THEN CloseLibrary(utilitybase)
  110.  
  111. ENDPROC
  112.  
  113.  
  114.  
  115.  
  116. /* init the "class" */
  117.  
  118. PROC pluginInit(tags:PTR TO tagitem)
  119.     /* do initializations that can't be done in main() here */
  120.     /* read files, open libs, etc. */
  121.  
  122.     IF (datatypesbase := OpenLibrary('datatypes.library', 39)) = NIL THEN RETURN FALSE
  123.  
  124.  
  125. ENDPROC TRUE
  126.  
  127.  
  128.  
  129.  
  130. /* free the "class" */
  131.  
  132. PROC pluginCleanup()
  133.     /* cleanup things done in pluginInit() */
  134.  
  135.     IF datatypesbase THEN CloseLibrary(datatypesbase)
  136.  
  137. ENDPROC
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. /* init an instance */
  148.  
  149. PROC pluginInitInstance(tags:PTR TO tagitem) HANDLE
  150.     DEF    h=NIL:PTR TO handle,
  151.         args=NIL:PTR TO LONG,
  152.         width, height
  153.  
  154.     /* this function is allways called by WBBump */
  155.     /* use it to allocate buffers, etc */
  156.  
  157.     /* return a handle to an "instance" */
  158.  
  159.  
  160.     /* alloc handle */
  161.     NEW h
  162.  
  163.  
  164.  
  165.     /* set global vars */
  166.  
  167.     IF tags
  168.         width        := GetTagData(PLUGINTAG_WIDTH, -1, tags)
  169.         height        := GetTagData(PLUGINTAG_HEIGHT, -1, tags)
  170.         h.args        := GetTagData(PLUGINTAG_ARGS, NIL, tags)
  171.         args        := [NIL, 0]
  172.         h.rdargs    := parseargs(ARGTEMPLATE, args, h.args)
  173.         IF h.rdargs = NIL THEN eThrow(-1, 'Bad arguments, template is %s', [ARGTEMPLATE])
  174.         h.picname    := args[0]
  175.         h.firstrun    := TRUE
  176.     ELSE
  177.         RETURN NIL
  178.     ENDIF
  179.  
  180.  
  181.     /* load image with cimg class */
  182.     IF getImage(h) = FALSE THEN eThrow(-1, 'Unable to load image "%s" with datatypes', [h.picname])
  183.  
  184.  
  185.     /* check size */
  186.     IF    (width <> -1) AND (width <> h.image.width) OR
  187.         (height <> -1) AND (height <> h.image.height)
  188.         eThrow(-1, 'Wrong size: %ldx%ld, for this image %ldx%ld is needed', [width, height, h.image.width, h.image.height])
  189.     ENDIF
  190.  
  191.     /* return NIL if there was an error */
  192.  
  193. EXCEPT DO
  194.     IF exception
  195.         IF h THEN pluginFreeInstance(h)
  196.         lasterr := exceptioninfo
  197.         RETURN NIL
  198.     ENDIF
  199. ENDPROC h
  200.  
  201.  
  202. /* proc to catch exceptions */
  203. PROC getImage(h:PTR TO handle) HANDLE
  204.     NEW h.image.newfromDT(h.picname)
  205. EXCEPT DO
  206.     IF exception THEN RETURN FALSE
  207. ENDPROC TRUE
  208.  
  209.  
  210.  
  211. /* free an instance */ 
  212.  
  213. PROC pluginFreeInstance(h:PTR TO handle)
  214.     DEF dummy
  215.  
  216.     /* cleanup stuff allocated in pluginInitInstance() */
  217.  
  218.     IF h.rdargs THEN freeargs(h.rdargs)
  219.  
  220.     END h.image
  221.  
  222. ENDPROC
  223.  
  224.  
  225.  
  226. /* get an attribute from the instance, or globally if handle = 0 */
  227.  
  228. PROC pluginGetAttr(h:PTR TO handle, attr, valueptr:PTR TO LONG)
  229.  
  230.     /* this is where WBBump gets information from the plugin */
  231.     /* look in plugin.e to see what is required */
  232.  
  233.     IF h
  234.         SELECT attr
  235.  
  236.         CASE PLUGINTAG_WIDTH
  237.             valueptr[0] := h.image.width    -> return the width
  238.             RETURN TRUE
  239.  
  240.         CASE PLUGINTAG_HEIGHT
  241.             valueptr[0] := h.image.height    -> return the height
  242.             RETURN TRUE
  243.  
  244.         CASE PLUGINTAG_NEEDUPDATE
  245.             IF h.firstrun
  246.                 valueptr[0] := TRUE
  247.                 h.firstrun := FALSE
  248.             ELSE
  249.                 valueptr[0] := FALSE
  250.             ENDIF
  251.             RETURN TRUE
  252.  
  253.         ENDSELECT
  254.     ENDIF
  255.  
  256.     SELECT attr
  257.  
  258.     CASE PLUGINTAG_WIDTH
  259.         valueptr[0] := -1
  260.         RETURN TRUE
  261.  
  262.     CASE PLUGINTAG_HEIGHT
  263.         valueptr[0] := -1
  264.         RETURN TRUE
  265.  
  266.     CASE PLUGINTAG_ISMODIFIER
  267.         valueptr[0] := ISMODIFIER    -> see top of file
  268.         RETURN TRUE
  269.  
  270.     CASE PLUGINTAG_COMMANDNAME
  271.         valueptr[0] := 'BUMPMAP'-> tooltypes command
  272.         RETURN TRUE
  273.  
  274.     CASE PLUGINTAG_ISSTATIC
  275.         valueptr[0] := ISSTATIC        -> see top of file
  276.         RETURN TRUE
  277.  
  278.     CASE PLUGINTAG_TYPE
  279.         valueptr[0] := PLUGINTYPE
  280.         RETURN TRUE
  281.  
  282.     CASE PLUGINTAG_NAME
  283.         valueptr[0] := 'image.wbbplugin'
  284.         RETURN TRUE
  285.  
  286.     CASE PLUGINTAG_COPYRIGHT
  287.         valueptr[0] := '©1999 Thomas Jensen - dm98411@edb.tietgen.dk'
  288.         RETURN TRUE
  289.  
  290.     CASE PLUGINTAG_AUTHOR
  291.         valueptr[0] := 'Thomas Jensen - dm98411@edb.tietgen.dk'
  292.         RETURN TRUE
  293.  
  294.     CASE PLUGINTAG_DESC
  295.         valueptr[0] := 'Image loader using datatypes\nImages must be 8bit'
  296.         RETURN TRUE
  297.  
  298.     CASE PLUGINTAG_LASTERROR
  299.         valueptr[0] := lasterr
  300.         RETURN TRUE
  301.  
  302.     DEFAULT
  303.         lasterr := 'Unknown tag'
  304.         RETURN FALSE            -> return false if the tag is unknown
  305.  
  306.     ENDSELECT
  307.  
  308. ENDPROC FALSE
  309.  
  310.  
  311.  
  312. PROC pluginSetAttr(h:PTR TO handle, attr, value)
  313.  
  314.     /* later this may be used to set plugin attributes */
  315.     /* if there's no attributes to set, return FALSE */
  316.  
  317. ENDPROC FALSE
  318.  
  319.  
  320.  
  321.  
  322. PROC pluginDoAction(h:PTR TO handle, inbuf:PTR TO CHAR, outbuf:PTR TO CHAR, tags:PTR TO tagitem) HANDLE
  323.     DEF    mybuf=NIL:PTR TO CHAR
  324.  
  325.     /* this is the "action" part of the plugin */
  326.     /* it is called each time the plugin's services are needed */
  327.     /* if the plugin says TRUE to ISMODIFIER then inbuf MAY contain an image */
  328.     /* othervise it a NIL pointer */
  329.     /* tags is currently not used */
  330.  
  331.     IF h.image.lock([CIMGTAG_LOCK_BUF, {mybuf}, TAG_END]) = FALSE THEN eThrow(-1, 'Unable to lock image (internal error)')
  332.  
  333.     CopyMem(mybuf, outbuf, h.image.width * h.image.height)
  334.  
  335.     h.image.unlock()
  336.  
  337.  
  338. EXCEPT DO
  339.     /* return FALSE if something went wrong */
  340.     IF exception
  341.         lasterr := exceptioninfo
  342.         RETURN FALSE
  343.     ENDIF
  344. ENDPROC TRUE
  345.